package com.xja.wechat.service.Imp;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayConstants;
import com.github.wxpay.sdk.WXPayUtil;
import com.xja.wechat.common.Constant;
import com.xja.wechat.common.DateUtil;
import com.xja.wechat.common.JsonBean;
import com.xja.wechat.domain.mapper.WX_OrderMapper;
import com.xja.wechat.domain.mapper.WX_UserMapper;
import com.xja.wechat.domain.model.WX_Order;
import com.xja.wechat.pay.MyWXPayConfig;
import com.xja.wechat.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private WX_OrderMapper wx_orderMapper;
//    @Autowired
//    private WX_UserMapper wx_userMapper;

    @Override
    public JsonBean addOrder(String openid) {
//        wx_userMapper.selectByOpenid(openid);
        JsonBean jsonBean = new JsonBean(-1, "创建订单失败", null);
        WX_Order wxOrder = new WX_Order();
        String orderNo = "WXDD" + DateUtil.getSdfTimes() + UUID.randomUUID().toString().substring(0, 3);
        wxOrder.setOrderno(orderNo);
        wxOrder.setPrice(1);
        wxOrder.setStatus("0");
        wxOrder.setOpenid(openid);
        int i = wx_orderMapper.insert(wxOrder);
        if (i > 0) {
            jsonBean = new JsonBean(0, "ok", orderNo);
        }
        System.out.println(jsonBean.toString());
        return jsonBean;
    }

    @Override
    public JsonBean payOrder(String orderNo, String openid) {
        //支付相关的内容
        WXPay wxPay = new WXPay(new MyWXPayConfig());
        Map<String, String> respMap = null;
        //调用统一下单API
        Map<String, String> reqData = new HashMap<>();//要发送出去的map
        reqData.put("body", "ZQJSAPI支付");//数据库查出来的DB select
        //订单不能写死  唯一的
        reqData.put("out_trade_no", orderNo);//商户订单号
        reqData.put("total_fee", "1");//标价金额  分  DB查出来的
        reqData.put("spbill_create_ip", "123.12.12.123");//用户终端IP
        reqData.put("notify_url", "http://www.scorpio-cc.com/wx/pay/notifyUrl");//通知地址
        reqData.put("trade_type", "JSAPI");//交易类型
        System.out.println("支付获取到的openid+" + openid);
        reqData.put("openid", openid);
        try {
            //发送出去返回一个map
            //统一下单
            respMap = wxPay.unifiedOrder(reqData);
            System.out.println(respMap.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, String> resultMap = new HashMap<>();
        resultMap.put("appId", Constant.APPID);
        resultMap.put("timeStamp", System.currentTimeMillis() / 1000 + "");
        resultMap.put("nonceStr", WXPayUtil.generateNonceStr());
        resultMap.put("package", "prepay_id=" + respMap.get("prepay_id").toString());
        resultMap.put("signType", "MD5");
        try {
            String signature = WXPayUtil.generateSignature(resultMap, "1234567890asdfghjklzxcvbnmqwerty", WXPayConstants.SignType.MD5);
            resultMap.put("paySign", signature);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new JsonBean(0, "ok", resultMap);
    }

    /**
     * 只要调用该接口就能知道订单状态
     *
     * @param orderNo
     * @return
     */
    @Override
    public JsonBean selectOrderStatus(String orderNo) {
        WX_Order wxOrder = wx_orderMapper.selectByOrderNo(orderNo);

        return new JsonBean(0, "ok", wxOrder.getStatus());
    }

    @Override
    public JsonBean updateOrderStatus(String orderNo, String orderStatus) {
        JsonBean jsonBean = new JsonBean(-1, "失败", null);
        WX_Order wxOrder = new WX_Order();
        wxOrder.setStatus(orderStatus);
        wxOrder.setOrderno(orderNo);
        int i = wx_orderMapper.updateOrderStatus(wxOrder);
        if (i > 0) {
            jsonBean = new JsonBean(0, "ok", null);
        }
        return jsonBean;
    }
}
